package com.example.listview2;

import java.util.ArrayList;

import com.example.listview2.MainActivity;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity 
{
	private static final int DELETE_WORK = Menu.FIRST;
	private static final int ABOUT = Menu.FIRST + 2;
	
	ArrayList<Work> array;
	ListWorkApdapter arrayAdapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        array = new ArrayList<Work>();
        arrayAdapter = new ListWorkApdapter(this, R.layout.list, array);
        final EditText txtCongviec = (EditText)findViewById(R.id.txtCongviec);
        final EditText txtGio = (EditText)findViewById(R.id.txtGio);
        final EditText txtPhut = (EditText)findViewById(R.id.txtPhut);
        final Button btnLuu = (Button)findViewById(R.id.btnLuu);
        
        final ListView listCV = (ListView)findViewById(R.id.listWiew);
        listCV.setAdapter(arrayAdapter);
        
        OnClickListener clickbtn = new OnClickListener() 
        {
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				if(txtCongviec.getText().toString().equals("")||txtGio.getText().toString().equals("") || txtPhut.getText().toString().equals(""))
				{
					AlertDialog.Builder canhBao = new AlertDialog.Builder(MainActivity.this);
					canhBao.setTitle("Cảnh báo");
					canhBao.setMessage("Nhập đẩy đủ thông tin");
					canhBao.setPositiveButton("Tiếp tục", new DialogInterface.OnClickListener() 
					{
						
						public void onClick(DialogInterface dialog, int which) 
						{
							// TODO Auto-generated method stub
							
						}
					});
					canhBao.show();
				}
				else
				{
					String congViec = txtCongviec.getText().toString() ;
					String thoiGian = txtGio.getText().toString() + " - " + txtPhut.getText().toString();
					Work work = new Work(congViec, thoiGian);
					
					array.add(0, work);
					arrayAdapter.notifyDataSetChanged();
					txtCongviec.setText("");
					txtGio.setText("");
					txtPhut.setText("");
				}
			}
		};
		btnLuu.setOnClickListener(clickbtn);
    }

    //tạo menu khi bấm nút menu trên dt
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        menu.add(0, DELETE_WORK, 0, "Xóa CV").setIcon(android.R.drawable.ic_delete);
        menu.add(0,ABOUT,0,"About").setIcon(android.R.drawable.ic_menu_info_details);
        return true;
    }
    
    //xử lý sự kiện trong menu
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch(item.getItemId())
    	{
    		case DELETE_WORK:
    		{
    			xoaItem();
    			break;
    		}
    		case ABOUT:
    		{
    			AlertDialog.Builder thongtin = new AlertDialog.Builder(this);
    			thongtin.setTitle("Thông Tin");
    			thongtin.setMessage("Người viết:" + "\n" + "Nguyễn Thanh Liêm");
    			thongtin.setPositiveButton("Thoát", new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) 
					{
						// TODO Auto-generated method stub
					}
				});
    			thongtin.setIcon(android.R.drawable.ic_menu_info_details);
    			thongtin.show();
    			break;
    		}
    	}
    	return true;
    }
    
    //hàm xóa item
    private void xoaItem()
    {
    	if(array.size()>0)
    	{
    		for(int i=0;i<array.size();i++)
    		{
    			if(i>array.size())
    			{
    				break;
    			}
    			if(array.get(i).isChecked())
    			{
    				array.remove(i);
    				arrayAdapter.notifyDataSetChanged();
    				continue;
    			}
    		}
    	}
    }
}
